package biz.impl;

import biz.UserBiz;
import entity.Users;
import entity.return_type.BooleanMsg;
import util.HibernateUtils;

public class UserBizImpl implements UserBiz {

	// 用户注册
	@Override
	public BooleanMsg userRegister(Users users) {
		BooleanMsg out = new BooleanMsg();
		try {
			Users u = new Users();
			u.setUsername(users.getUsername());
			if(HibernateUtils.getByHql(HibernateUtils.createQueryHql(u)).size() > 0){
				out.setSuccess(false);
				out.setMsg("该用户名已经存在");
			} else {
				HibernateUtils.addOne(users);
				out.setSuccess(true);
				out.setMsg("登陆成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}

	// 用户登录
	@Override
	public Users userLogin(String username, String password) {
		Users users = null;
		String hql = "from Users where username = ? and password = ?";
		Object[] params = {username, password};
		try {
			users = (Users) HibernateUtils.getByHql(hql, params).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}

}
